//
//  LoginViewController.m
//  Baymax
//
//  Created by jolin on 16/4/19.
//  Copyright © 2016年 jolin. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "BMNavBar.h"
#import "Header.h"
#import "RequestAPI.h"
#import "UserInfo.h"

@interface LoginViewController () {
    UITextField *_userNameTF;
    UITextField *_pswTF;
}

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"登录页面");
     BMNavBar *loginNav = [[BMNavBar alloc]initWithItems:@[@"登录"] navBarStyle:NavBarStyleDefault];
    [self.view addSubview:loginNav];
    
    //添加view
    [self initView];
}

#pragma mark - 初始化view
- (void)initView {
    UITextField *usernameTF = [[UITextField alloc]initWithFrame:CGRectMake(0, 300, MAINFRAME.size.width, 40)];
    usernameTF.placeholder = @"手机号码";
    usernameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    usernameTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.view addSubview:usernameTF];
    
    _userNameTF = usernameTF;
    
    UITextField *pswTF = [[UITextField alloc]initWithFrame:CGRectMake(0, 341, MAINFRAME.size.width, 40)];
    pswTF.placeholder = @"密码";
    pswTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    pswTF.secureTextEntry = YES;
    [self.view addSubview:pswTF];
    _pswTF = pswTF;
    
    //登录按钮
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(10, 401, MAINFRAME.size.width - 20, 35);
    loginBtn.backgroundColor = [UIColor blueColor];
    loginBtn.layer.cornerRadius = 5;
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    
    //注册按钮
    UIButton *regBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    regBtn.frame = CGRectMake(MAINFRAME.size.width - 220, 446, 200, 20);
    [regBtn setTitle:@"注册新用户" forState:UIControlStateNormal];
    [regBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [regBtn addTarget:self action:@selector(regBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:regBtn];
    
}

#pragma mark - 键盘下去事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
#pragma mark - 登录按钮点击事件
- (void)loginBtnClick:(UIButton *)sender {
    //检查数据
    if ([_userNameTF.text isEqualToString:@""]) {
        [CommenTools alertPromptWithView:self.view prompt:@"用户名不能为空"];
        return;
    }
    if ([_pswTF.text isEqualToString:@""]) {
        [CommenTools alertPromptWithView:self.view prompt:@"密码不能为空"];
        return;
    }
    //请求登录
    RequestAPI *reqAPI = [[RequestAPI alloc]init];
    [reqAPI userLoginWithUsername:_userNameTF.text password:_pswTF.text result:^(NSArray *arr, NSError *error) {
        //判断登录成功or失败 进行跳转或提示
        if (arr.count != 0) {
            NSDictionary *userDic = [arr firstObject];
            UserInfo *user = [UserInfo userInfoWithDic:userDic];
            user.password = _pswTF.text;
            [DataBaseTool insertUserInfo:user];
            
            //登录成功 将用户信息存入本地
            [[NSUserDefaults standardUserDefaults] setValue:userDic[@"token"] forKey:@"accessToken"];
            [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"userStatus"];
            
            //跳转
            UITabBarController *tabBarVC = [CommenTools setUpTabBar];
            self.view.window.rootViewController = tabBarVC;
            
        }else {
            [CommenTools alertPromptWithView:self.view prompt:@"登录失败"];
            NSLog(@"登录失败");
        }

    }];
  }

#pragma mark - 注册按钮点击事件
- (void)regBtnClick:(UIButton *)sender {
    //跳转到注册页面
    RegistViewController *regVC = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:regVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
